package org.jeecg.modules.clubEventManagement.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.modules.clubEventManagement.entity.ClubEventManagement;
import org.jeecg.modules.clubEventManagement.entity.ClubEventManagementResult;
import org.jeecg.modules.clubEventManagement.entity.EventRegistrationNumEntity;
import org.jeecg.modules.clubEventManagement.mapper.ClubEventManagementMapper;
import org.jeecg.modules.clubEventManagement.service.IClubEventManagementService;
import org.jeecg.modules.communityInfluence.entity.CommunityInfluence;
import org.jeecg.modules.ruleOfInfluence.entity.RuleOfInfluence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Description: 社团活动管理
 * @Author: jeecg-boot
 * @Date: 2020-07-20
 * @Version: V1.0
 */
@Service
public class ClubEventManagementServiceImpl extends ServiceImpl<ClubEventManagementMapper, ClubEventManagement> implements IClubEventManagementService {

    @Autowired
    ClubEventManagementMapper clubEventManagementMapper;

    /**
     * 社团活动管理列表，分页查询
     *
     * @param clubEventManagementResult
     * @param eventManagementResult
     * @return
     */
    @Override
    public Page<ClubEventManagementResult> getList(Page<ClubEventManagementResult> clubEventManagementResult, ClubEventManagementResult eventManagementResult) {
        return clubEventManagementResult.setRecords(clubEventManagementMapper.getList(eventManagementResult));
    }

    /**
     * 活动审核
     *
     * @param clubEventManagement
     * @return
     */
    @Override
    @Transactional
    public Boolean activityReview(ClubEventManagement clubEventManagement) {
        Boolean bool;
        // 活动审核
        bool = clubEventManagementMapper.activityReview(clubEventManagement);

        if (bool) {
            // 获取社团影响力规则
            RuleOfInfluence ruleOfInfluence = clubEventManagementMapper.getRuleOfInfluence();
            // 根据社团ID查询社团影响力
            CommunityInfluence communityInfluence = clubEventManagementMapper.getInfluenceInfo(clubEventManagement.getClubId());

            if (communityInfluence.getTotalInfluence() >= ruleOfInfluence.getLevelTwoInfluence()) {
                // 当前影响力大于等于LV2所需要影响力则更新社团影响力等级
                clubEventManagementMapper.updateLevel("2");
            } else if (communityInfluence.getTotalInfluence() >= ruleOfInfluence.getLevelThreeInfluence()) {
                // 当前影响力大于等于LV3所需要影响力则更新社团影响力等级
                clubEventManagementMapper.updateLevel("3");
            }
            int i = 0;

            // 获取当天时间
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String dateTime = sdf.format(date);
            Integer num = clubEventManagementMapper.getMostDayDynamic(clubEventManagement.getClubId(), dateTime);
            int sum = num * ruleOfInfluence.getSingleDynamic();
            if (sum >= ruleOfInfluence.getMostDayDynamic()) {
                // 如果当天发布动态影响力总值大于等于规则值则影响力不增加
                return bool;
            } else {
                if ((sum + ruleOfInfluence.getSingleDynamic()) > ruleOfInfluence.getMostDayDynamic()) {
                    // 当天发布动态还能增加多少影响力
                    int j = ruleOfInfluence.getMostDayDynamic() - sum;
                    i = communityInfluence.getTotalInfluence() + j;
                    // 更新影响力
                    communityInfluence.setTotalInfluence(i);
                    bool = clubEventManagementMapper.updateInfluence(communityInfluence);
                    return bool;
                } else {
                    // 发布动态审核通过后社团总影响力
                    i = ruleOfInfluence.getSingleDynamic() + communityInfluence.getTotalInfluence();
                    // 更新影响力
                    communityInfluence.setTotalInfluence(i);
                    bool = clubEventManagementMapper.updateInfluence(communityInfluence);
                    return bool;
                }
            }
        }
        return bool;
    }

    /**
     * 社团活动报名
     *
     * @param form
     * @return
     */
    @Override
    @Transactional
    public Boolean addNumberOfApplicants(EventRegistrationNumEntity form) {
        Boolean bool;
        form.setId(UUIDGenerator.generate());
        form.setCreateTime(new Date());
        form.setUpdateTime(new Date());
        // TODO 暂时使用后台用户的登陆信息，后期手机用户与后台用户区分开
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        form.setCreateBy(sysUser.getUsername());
        form.setUpdateBy(sysUser.getUsername());
        bool = clubEventManagementMapper.addNumberOfApplicants(form);
        if (bool) {
            // 获取社团影响力规则
            RuleOfInfluence ruleOfInfluence = clubEventManagementMapper.getRuleOfInfluence();
            // 根据社团ID查询社团影响力
            CommunityInfluence communityInfluence = clubEventManagementMapper.getInfluenceInfo(form.getSocialId());
            // TODO 还缺一个对个人对社团影响力贡献，现在暂时还没有表，等添加app用户表再添加



            int i = 0;
            // 当前社团影响力值
            i = ruleOfInfluence.getMemberRegistration() + communityInfluence.getTotalInfluence();
            // 成员报名参加活动更新社团影响力
            communityInfluence.setTotalInfluence(i);
            bool = clubEventManagementMapper.updateInfluence(communityInfluence);

            if (communityInfluence.getTotalInfluence() >= ruleOfInfluence.getLevelTwoInfluence()) {
                // 当前影响力大于等于LV2所需要影响力则更新社团影响力等级
                clubEventManagementMapper.updateLevel("2");
            } else if (communityInfluence.getTotalInfluence() >= ruleOfInfluence.getLevelThreeInfluence()) {
                // 当前影响力大于等于LV3所需要影响力则更新社团影响力等级
                clubEventManagementMapper.updateLevel("3");
            }

            return bool;
        }
        return bool;
    }


}
